//
//  YZMStockSellCell.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/28.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMStockSellCell.h"
#import "UIImage+Utils.h"
#import "YZMSaleTradeModel.h"
#import "NSNumber+Utils.h"
#import "YZMConfigureService.h"
#import "YZMStockRealModel.h"
#import "UILabel+Color.h"
#import "YZMAlertView.h"
#import "YZMSeverTimeService.h"
#import "YZMConfigureService.h"
#import <DateTools/DateTools.h>
#import <BlocksKit/UIControl+BlocksKit.h>

@interface YZMStockSellCell()

@property (nonatomic, strong) UILabel *nameLB;
@property (nonatomic, strong) UIImageView *imageViewRisk;
@property (nonatomic, strong) UILabel *codeLB;
@property (nonatomic, strong) UILabel *statusLB;
@property (nonatomic, strong) UIButton *operBT;
@property (nonatomic, strong) UILabel *profitLB;
@property (nonatomic, strong) UILabel *costPriceLB;
@property (nonatomic, strong) UILabel *currentPriceLB;
@property (nonatomic, strong) UILabel *amountLB;
@property (nonatomic, strong) UILabel *stopMoneyLB;

@property (nonatomic, strong) UILabel *delayConditionLB;
@property (nonatomic, strong) UILabel *riskLB;
@property (nonatomic, strong) UILabel *sequenceLB;
@property (nonatomic, strong) UILabel *timeLB;

@property (nonatomic, strong) UIView *stopFlagView;
@property (nonatomic, strong) UIButton *oweBT;
@property (nonatomic, strong) UIButton *agreementBT;

@property (nonatomic, strong) UIButton *apartBT;
@property (nonatomic, strong) UIView *stopView;
@property (nonatomic, strong) UIButton *giveUpBtn;
@property (nonatomic, strong) UIButton *buyOverBtn;
@property (nonatomic, strong) YZMConfigureService *service;

@end

@implementation YZMStockSellCell


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView *bgView = [UIView new];
        bgView.backgroundColor = [YZMAppStyle colorForMain];
        self.backgroundView = bgView;
        [self createObjects];
    }
    
    return self;
}

- (YZMConfigureService*)service {
    if (!_service) {
        _service = [YZMConfigureService sharedServiceWithTradeType:self.tradeType];
    }
    return _service;
}

- (void)setRecord:(YZMSaleTradeModel *)record{
    
    _record = record;
    if([@(record.owedMoney) isGreaterThanDouble:0]){
        
        self.oweBT.hidden = NO;
    }else{
        self.oweBT.hidden = YES;
    }
    
    self.nameLB.text = record.stockName;
    self.codeLB.text = record.stockCode;
    self.stopFlagView.hidden=YES;
    self.profitLB.text = @"--";
    self.statusLB.hidden=NO;
    
    self.stopView.hidden=YES;
    self.operBT.hidden = YES;
    self.stopMoneyLB.text = @(record.quitLoss).p0fString;
    
    self.sequenceLB.text = [NSString stringWithFormat:@"JQ%@", @(record.tid)];
    self.timeLB.text = [record.dealTime formattedDateWithFormat:@"YYYY-MM-dd HH:mm:ss"];
    self.agreementBT.hidden = NO;
    self.currentPriceLB.text=@"--";
    self.delayConditionLB.superview.hidden = YES;
    [self.sequenceLB.superview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.delayConditionLB.superview);
    }];
    
    if (record.holdCount != 0) {
        self.amountLB.text=[NSString stringWithFormat:@"%@股", @(record.holdCount)];
    }else{
        self.amountLB.text = @"--";
    }
    
    if ([@(_record.buyPriceDeal) isGreaterThanDouble:0.0]) {
        self.costPriceLB.text = @(record.buyPriceDeal).p02fString;
    }else{
        self.costPriceLB.text = @"--";
        
    }
    
    if (![self.service isInTradeTimePeriods]) {
        self.operBT.hidden = YES;
        self.stopView.hidden=YES;
        self.stopFlagView.hidden=YES;
        self.statusLB.text = @"非交易时间段";
        self.statusLB.textColor = [YZMAppStyle colorForGrayText];
    }
    
    if (self.tradeType!=YZM_TRADE_TYPE_FOR_A_STOCK) {
        _agreementBT.hidden=YES;
    }
    
    switch (record.status) {
        case YZMSaleTradeStausForMatching:
            self.timeLB.text = @"--";
            self.agreementBT.hidden=YES;
            self.statusLB.text = @"等待接单";
            
            break;
            
        case YZMSaleTradeStausForMatched:
        case YZMSaleTradeStausForTrading:
            self.timeLB.text = @"--";
            self.agreementBT.hidden=YES;
            self.statusLB.text = @"正在委托买入";
            break;
            
        case YZMSaleTradeStausForHolding:{
            self.statusLB.hidden=YES;
            self.operBT.hidden=NO;
            if ([record.dealTime isSameDay:[YZMSeverTimeService sharedService].severDate]) {
                self.operBT.enabled = NO;
                self.operBT.backgroundColor = [YZMAppStyle colorForGrayUnable];
            }else{
                self.operBT.enabled = YES;
                self.operBT.backgroundColor = [YZMAppStyle colorForYellowButton];
            }
            if (self.tradeType==YZM_TRADE_TYPE_FOR_A_STOCK) {
                self.delayConditionLB.superview.hidden = NO;
                self.delayConditionLB.text = [@"浮动盈亏 > " stringByAppendingString:@(record.deferProfit).p0fString];
                 self.riskLB.hidden = ![[YZMConfigureService sharedService] isInLimitStockListWithStockCode:record.stockCode];
                [self.sequenceLB.superview mas_updateConstraints:^(MASConstraintMaker *make) {
              make.top.equalTo(self.delayConditionLB.superview).offset(20);
                }];
            }
        }
            break;
            
        case YZMSaleTradeStausForToClose:
        case YZMSaleTradeStausForClosing:
            self.statusLB.text = @"正在委托卖出";
            break;
            
        case YZMSaleTradeStausForDating:
            self.statusLB.text = @"正在清算";
            break;
            
        default:
            break;
    }
    
    if (record.tradingList.count == 0) {
        self.apartBT.hidden = YES;
    }else{
        self.apartBT.hidden = NO;
    }
}

- (void)setRealRecord:(YZMStockRealModel *)realRecord{
    
    self.statusLB.hidden=NO;
    _stopView.hidden=YES;
    _stopFlagView.hidden=YES;
    _operBT.hidden=YES;

    if(!realRecord){
        self.currentPriceLB.text = @"0";
        self.profitLB.text = @"0";
        self.statusLB.text=@"非交易时间段";
        _statusLB.textColor=[YZMAppStyle colorForGrayText];
        _currentPriceLB.textColor=[YZMAppStyle colorForGrayText];
        _profitLB.textColor=[YZMAppStyle colorForGrayText];
    }else{
    //非交易时间段
    self.imageViewRisk.hidden = ![[YZMConfigureService sharedService] isInLimitStockListWithStockCode:realRecord.code];
    if (![self.service isInTradeTimePeriods]) {
        self.statusLB.text=@"非交易时间段";
        self.statusLB.textColor=[YZMAppStyle colorForGrayText];
        self.operBT.hidden=YES;
        self.stopFlagView.hidden=YES;
        if (realRecord.isSuspended) {
            
            self.stopFlagView.hidden=NO;
            self.imageViewRisk.hidden = YES;
            self.riskLB.hidden = YES;
        }
    }else{
        self.statusLB.textColor=[YZMAppStyle colorForYellowButton];

        switch (_record.status) {
            case YZMSaleTradeStausForMatching:
                self.statusLB.text = @"等待接单";
                break;
                
            case YZMSaleTradeStausForMatched:
            case YZMSaleTradeStausForTrading:
                self.statusLB.text = @"正在委托买入";
                break;
                
            case YZMSaleTradeStausForHolding:
                self.statusLB.hidden=YES;
                _operBT.hidden=YES;
                if (realRecord.isSuspended) {
                    if (self.tradeType== YZM_TRADE_TYPE_FOR_A_STOCK || self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_YLB) {
                        _stopView.hidden=NO;
                    }else{
                        _stopView.hidden=YES;
                        _operBT.hidden=NO;
                        _operBT.enabled=NO;
                    }
                    
                    _stopFlagView.hidden=NO;
                    self.imageViewRisk.hidden = YES;
                    self.riskLB.hidden = YES;
                }else{
                    _stopView.hidden=YES;
                    _stopFlagView.hidden=YES;
                    _operBT.hidden=NO;
                    if ([_record.dealTime isSameDay:[YZMSeverTimeService sharedService].severDate] ) {
                        _operBT.enabled=NO;
                    }else{
                        _operBT.enabled=YES;
                    }
                }
                break;
                
            case YZMSaleTradeStausForToClose:
            case YZMSaleTradeStausForClosing:
                self.statusLB.text = @"正在委托卖出";
                break;
                
            case YZMSaleTradeStausForDating:
                self.statusLB.text = @"正在清算";
                self.profitLB.text=@"0";
                self.currentPriceLB.text = @(realRecord.price).p02fString;
                [self.currentPriceLB setTextColorWithGreatNumber:realRecord.price andSmallNumber:_record.buyPriceDeal];
                break;
                
            default:
                break;
        }
    }
    if (realRecord.isSuspended) {
        self.currentPriceLB.text = @(realRecord.lastClosePrice).p02fString;
        self.currentPriceLB.textColor = [YZMAppStyle colorForGrayText];
        double sum = (realRecord.lastClosePrice - _record.buyPriceDeal)*_record.holdCount;
        [self.profitLB setTextColorWithGreatNumber:sum andSmallNumber:0];
        NSString *profitString;
        if ([@(sum) isGreaterThanDouble:0.0]) {
            profitString=[NSString stringWithFormat:@"+%@",@(sum).p02fString];
        }else{
            profitString=@(sum).p02fString;
        }
        self.profitLB.text = profitString;
        _stopFlagView.hidden=NO;
        self.imageViewRisk.hidden = YES;
        self.riskLB.hidden = YES;
    }else{
        _stopFlagView.hidden=YES;
        self.currentPriceLB.text = @(realRecord.price).p02fString;
        [self.currentPriceLB setTextColorWithGreatNumber:realRecord.price andSmallNumber:[@(_record.buyPriceDeal) isEqualWithDouble:0]?realRecord.price:_record.buyPriceDeal];
        //如果总持仓量不为0，计算实时浮动盈亏。
        if (_record.holdCount != 0) {
            double sum = (realRecord.price - _record.buyPriceDeal)*_record.holdCount;
            [self.profitLB setTextColorWithGreatNumber:sum andSmallNumber:0];
            NSString *profitString;
            if ([@(sum) isGreaterThanDouble:0.0]) {
                profitString=[NSString stringWithFormat:@"+%@",@(sum).p02fString];
            }else{
                profitString=@(sum).p02fString;
            }
            self.profitLB.text = profitString;
            
            if (_record.status<=3) {
                self.profitLB.text=@"--";
                self.profitLB.textColor=[YZMAppStyle colorForGrayText];
            }
        }else{
            self.profitLB.text = @"0";
            self.profitLB.textColor = [YZMAppStyle colorForGrayText];
        }
      }
    }

    NSInteger year = [[[YZMSeverTimeService sharedService].severDate formattedDateWithFormat: @"yyyy"] integerValue];
    NSInteger month = [[[YZMSeverTimeService sharedService].severDate formattedDateWithFormat: @"MM"] integerValue];
    NSInteger day = [[[YZMSeverTimeService sharedService].severDate formattedDateWithFormat: @"dd"] integerValue];
    
    NSDate *beginDate = [NSDate dateWithYear:year month:month day:day hour:[@(9) integerValue] minute:[@(00) integerValue]second:0];
    NSDate *endDate = [NSDate dateWithYear:year month:month day:day hour:[@(9) integerValue] minute:[@(30) integerValue]second:0];
    
    DTTimePeriod *timePeriod = [DTTimePeriod timePeriodWithStartDate:beginDate endDate:endDate];
    
    if (![timePeriod containsDate:[YZMSeverTimeService sharedService].severDate interval:DTTimePeriodIntervalClosed] && realRecord.isSuspended  && (self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK || self.tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_YLB)) {
        
        _stopView.hidden=NO;
        _stopFlagView.hidden=NO;
        self.imageViewRisk.hidden = YES;
        self.riskLB.hidden = YES;
        _statusLB.hidden = YES;
        
    }
    
    if (self.tradeType!=YZM_TRADE_TYPE_FOR_A_STOCK) {
        self.costPriceLB.textColor=[YZMAppStyle colorForWhite];
        self.amountLB.textColor=[YZMAppStyle colorForWhite];
        self.stopMoneyLB.textColor=[YZMAppStyle colorForWhite];
    }
}

#pragma mark -- event

- (void)clickToShowAgreement:(UIButton *)sender{
    
    if (self.agreementAction) {
        
        self.agreementAction();
    }
}

- (void)clickToShowOwe:(UIButton *)sender{
    
    if (self.oweAction) {
        
        self.oweAction();
    }
}

- (void)clickToOper:(UIButton *)sender{
    
    if (self.sellAction) {
        
        self.sellAction();
    }
}


-(void)giveup:(UIButton *)sender
{
    if (self.giveupBlock) {
        self.giveupBlock();
    }
}

-(void)buyOver:(UIButton *)sender
{
    if (self.buyoverBlock) {
        self.buyoverBlock();
    }
}

#pragma mark -- ui

- (void)createObjects{
    
    //股票名称
    self.nameLB = [YZMAppStyle labelWithFont:[YZMAppStyle largeSizeFont] andColor:[YZMAppStyle colorForMainTextColor]];
    [self.contentView addSubview:self.nameLB];
    [self.nameLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(12);
    }];
    
    self.stopFlagView = [UIView new];
    self.stopFlagView.layer.borderWidth = 1;
    self.stopFlagView.layer.borderColor = [YZMAppStyle colorForYellowButton].CGColor;
    [self.contentView addSubview:self.stopFlagView];
    self.stopFlagView.hidden = YES;
    [self.stopFlagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLB.mas_right).offset(5);
        make.centerY.equalTo(self.nameLB);
    }];
    
    self.imageViewRisk = [[UIImageView alloc] init];
    self.imageViewRisk.image = [UIImage imageNamed:@"risk.png"];
    self.imageViewRisk.hidden = YES;
    [self.contentView addSubview:self.imageViewRisk];
    [self.imageViewRisk mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLB.mas_right).offset(5);
        make.centerY.equalTo(self.nameLB);
        if (iPhone5 || iPhone4) {
            make.width.equalTo(@30);
            make.height.equalTo(@16);
        }else{
            make.width.equalTo(@40);
            make.height.equalTo(@16);
        }
        
    }];
    
    UILabel *stopFlagLB = [YZMAppStyle labelWithFont:[YZMAppStyle fontWithSize:10.0] andColor:[YZMAppStyle colorForYellowButton]];
    stopFlagLB.text = @"停牌";
    [self.stopFlagView addSubview:stopFlagLB];
    [stopFlagLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.stopFlagView).insets(UIEdgeInsetsMake(2, 4, 2, 4));
    }];
    
    
    
    self.codeLB = [YZMAppStyle labelWithFont:[YZMAppStyle fontWithSize:14.0] andColor:[YZMAppStyle colorForGrayText]];
    [self.contentView addSubview:self.codeLB];
    [self.codeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.nameLB);
        make.top.equalTo(self.nameLB.mas_bottom).offset(4);
    }];
    
    UILabel *profitStringLB = [YZMAppStyle labelWithFont:[YZMAppStyle largeSizeFont] andColor:[YZMAppStyle colorForGrayText]];
    profitStringLB.text = @"浮动盈亏";
    [self.contentView addSubview:profitStringLB];
    [profitStringLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLB);
        make.centerX.equalTo(self.contentView);
    }];
    
    self.profitLB = [YZMAppStyle labelWithFont:[YZMAppStyle fontWithSize:14.0] andColor:[YZMAppStyle colorForGrayText]];
    [self.contentView addSubview:self.profitLB];
    [self.profitLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.codeLB);
        make.centerX.equalTo(self.contentView);
    }];
    
    self.stopView=[UIView new];
    [self.contentView addSubview:_stopView];
    [_stopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView).offset(-15);
        make.centerY.mas_equalTo(self.nameLB);
        make.height.mas_equalTo(30);
        make.left.mas_equalTo(profitStringLB.mas_right).offset(36);
    }];
    
    self.giveUpBtn=[YZMAppStyle submitButton];
    [_giveUpBtn setTitle:@"放弃" forState:UIControlStateNormal];
    [self.stopView addSubview:_giveUpBtn];
    [_giveUpBtn addTarget:self action:@selector(giveup:) forControlEvents:UIControlEventTouchUpInside];
    _giveUpBtn.enabled=YES;
    [_giveUpBtn.titleLabel setFont:[YZMAppStyle mediumSizeFont]];
    if (iPhone4 || iPhone5 || !(iPhone6 || iPhone6P)) {
        [_giveUpBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.top.height.equalTo(_stopView);
            make.width.mas_equalTo(40);
        }];
    }else{
        [_giveUpBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.top.height.equalTo(_stopView);
            make.width.mas_equalTo(50);
        }];
    }
    
    self.buyOverBtn=[YZMAppStyle submitButton];
    [_buyOverBtn setTitle:@"买断" forState:UIControlStateNormal];
    [_stopView addSubview:_buyOverBtn];
    [_buyOverBtn addTarget:self action:@selector(buyOver:) forControlEvents:UIControlEventTouchUpInside];
    
    _buyOverBtn.enabled=YES;
    [_buyOverBtn.titleLabel setFont:[YZMAppStyle mediumSizeFont]];
    if (iPhone5 || iPhone4) {
        [_buyOverBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.height.width.equalTo(_giveUpBtn);
            make.right.equalTo(_giveUpBtn.mas_left).offset(-5);
        }];
    }else{
        [_buyOverBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.height.width.equalTo(_giveUpBtn);
            make.right.equalTo(_giveUpBtn.mas_left).offset(-5);
        }];
    }
    _stopView.hidden=YES;

    
    self.statusLB = [YZMAppStyle labelWithFont:[YZMAppStyle fontWithSize:14.0] andColor:[YZMAppStyle colorForYellowButton]];
    [self.contentView addSubview:self.statusLB];
    [self.statusLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(self.nameLB);
    }];
    
    self.operBT = [UIButton new];
    self.operBT.hidden = YES;
//    self.operBT.layer.cornerRadius = 3.0;
//    self.operBT.layer.masksToBounds = YES;
    [YZMSetBorderAndCornerRadius setView:self.operBT borderWidth:0 borderColor:nil cornerRadius:3.0];
    [self.operBT setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.operBT setTitleColor:[YZMAppStyle colorForGrayText] forState:UIControlStateDisabled];
    [self.operBT setBackgroundColor:[YZMAppStyle colorForYellowButton]];
    [self.operBT.titleLabel setFont:[YZMAppStyle fontWithSize:14.0]];
    self.operBT.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    [self.operBT setTitle:@"点卖" forState:UIControlStateNormal];
//    [self.operBT setBackgroundImage:[UIImage createImageWithColor:[YZMAppStyle colorForGrayUnable]] forState:UIControlStateDisabled];
    [self.operBT addTarget:self action:@selector(clickToOper:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.operBT];
    [self.operBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLB);
        make.right.equalTo(self.contentView).offset(-15);
        make.height.mas_equalTo(30);
        make.width.mas_greaterThanOrEqualTo(60);
    }];
    
    
    UIView *splitLine1 = [YZMAppStyle splitLine];
    [self.contentView addSubview:splitLine1];
    [splitLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(self.codeLB.mas_bottom).offset(10);
    }];
    
    
    UIView *secondView = [self secondPartView];
    [self.contentView addSubview:secondView];
    [secondView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(splitLine1.mas_bottom).offset(10);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
    }];
    
    UIView *splitLine2 = [YZMAppStyle splitLine];
    [self.contentView addSubview:splitLine2];
    [splitLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(secondView.mas_bottom).offset(10);
    }];
    
    UIView *thirdView = [self thirdPartView];
    [self.contentView addSubview:thirdView];
    [thirdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(splitLine2.mas_bottom).offset(20);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
    }];
}


- (UIView *)secondPartView{
    
    UIView *view = [UIView new];
    
    UILabel *cbjLB = [YZMAppStyle labelWithMediumGray];
    cbjLB.text = @"成本价";
    [view addSubview:cbjLB];
    [cbjLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(view);
        make.width.equalTo(view).dividedBy(4).offset(-10);
    }];
    
    self.costPriceLB = [YZMAppStyle labelWithMediumGray];
    self.costPriceLB.text = @"--";
    [view addSubview:self.costPriceLB];
    [self.costPriceLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cbjLB);
        make.top.equalTo(cbjLB.mas_bottom).offset(5);
        make.bottom.equalTo(view);
    }];
    
    UILabel *dqjLB = [YZMAppStyle labelWithMediumGray];
    dqjLB.text = @"当前价";
    dqjLB.textAlignment = NSTextAlignmentCenter;
    [view addSubview:dqjLB];
    [dqjLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view);
        make.left.equalTo(cbjLB.mas_right);
        make.width.equalTo(view).dividedBy(4).offset(10);
    }];
    
    self.currentPriceLB = [YZMAppStyle labelWithMediumGray];
    self.currentPriceLB.text = @"--";
    [view addSubview:self.currentPriceLB];
    [self.currentPriceLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.costPriceLB);
        make.centerX.equalTo(dqjLB);
    }];
    
    UIView *helperView = [UIView new];
    [view addSubview:helperView];
    [helperView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(view);
        make.left.equalTo(dqjLB.mas_right);
        make.width.equalTo(view).dividedBy(4).offset(15);
    }];
    
    UILabel *slLB = [YZMAppStyle labelWithMediumGray];
    slLB.text = @"数量";
    slLB.textAlignment = NSTextAlignmentCenter;
    [helperView addSubview:slLB];
    [slLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.centerX.equalTo(helperView);
    }];
    
    self.apartBT = [YZMAppStyle explainButton];
    self.apartBT.hidden = YES;
    [self.apartBT bk_addEventHandler:^(id sender) {
        
        if (_record.tradingList.count != 0) {
            
            [[self alertViewForApartSellWithList:_record.tradingList andTotalAmount:_record.holdCount+_record.apartCount andApartAmount:_record.holdCount] showInView:YZMHCMInstance.currentNavationCtroller.view];
        }
        
    } forControlEvents:UIControlEventTouchUpInside];
    [helperView addSubview:self.apartBT];
    [self.apartBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(slLB);
        make.left.equalTo(slLB.mas_right);
    }];
    
    self.amountLB = [YZMAppStyle labelWithMediumGray];
    self.amountLB.text = @"--";
    
    [view addSubview:self.amountLB];
    [self.amountLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.costPriceLB);
        make.centerX.equalTo(slLB);
    }];
    
    UILabel *zsLB = [YZMAppStyle labelWithMediumGray];
    zsLB.text = @"止损";
    zsLB.textAlignment = NSTextAlignmentRight;
    [view addSubview:zsLB];
    [zsLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view);
        make.right.equalTo(view);
        make.width.equalTo(view).dividedBy(4).offset(-15);
    }];
    
    self.stopMoneyLB = [YZMAppStyle labelWithMediumGray];
    self.stopMoneyLB.text = @"--";
    [view addSubview:self.stopMoneyLB];
    [self.stopMoneyLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.costPriceLB);
        make.right.equalTo(zsLB);
    }];
    
    return  view;
}

- (UIView *)thirdPartView{
    
    UIView *view = [UIView new];
    
    UIView *delayView = [UIView new];
    [view addSubview:delayView];
    [delayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(view);
    }];
    
    UILabel *delayStringLB = [YZMAppStyle labelWithSmallLightGray];
    delayStringLB.text = @"递延条件";
    [delayView addSubview:delayStringLB];
    [delayStringLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(delayView);
    }];
    
    UIView *sequenceView = [UIView new];
    [view addSubview:sequenceView];
    [sequenceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(delayView).offset(20);
        make.left.equalTo(view);
    }];
    
    UILabel *sequenceStringLB = [YZMAppStyle labelWithSmallLightGray];
    sequenceStringLB.text = @"策略交易号";
    [sequenceView addSubview:sequenceStringLB];
    [sequenceStringLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(sequenceView);
        make.left.bottom.equalTo(sequenceView);
    }];
    
    self.sequenceLB = [YZMAppStyle labelWithSmallLightGray];
    [sequenceView addSubview:self.sequenceLB];
    [self.sequenceLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sequenceStringLB.mas_right).offset(10);
        make.top.right.equalTo(sequenceView);
    }];
    
    
    self.delayConditionLB = [YZMAppStyle labelWithFont:[YZMAppStyle smallSizeFont] andColor:[YZMAppStyle colorForYellowButton]];
    [delayView addSubview:self.delayConditionLB];
    [self.delayConditionLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sequenceLB);
        make.top.equalTo(delayView);
    }];
    
    self.riskLB = [YZMAppStyle labelWithFont:[YZMAppStyle smallSizeFont] andColor:[YZMAppStyle colorForYellowButton]];
    self.riskLB.text = @"风险股票无法递延";
    [delayView addSubview:self.riskLB];
    [self.riskLB mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.delayConditionLB.mas_right).offset(5);
        make.top.right.equalTo(delayView);
    }];
    
    UILabel *timeStringLB = [YZMAppStyle labelWithSmallLightGray];
    timeStringLB.text = @"成交时间";
    [view addSubview:timeStringLB];
    [timeStringLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view);
        make.top.equalTo(sequenceView).offset(20);
        make.bottom.equalTo(view);
    }];
    
    self.timeLB = [YZMAppStyle labelWithSmallLightGray];
    [view addSubview:self.timeLB];
    [self.timeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sequenceLB);
        make.top.equalTo(timeStringLB);
    }];
    
    self.oweBT = [YZMAppStyle explainButton];
    [self.oweBT setImage:[UIImage imageNamed:@"icon_stock_owe"] forState:UIControlStateNormal];
    [self.oweBT addTarget:self action:@selector(clickToShowOwe:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.oweBT];
    [self.oweBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sequenceLB.mas_right);
        make.centerY.equalTo(self.sequenceLB);
    }];
    
    
    self.agreementBT = [UIButton new];
    [self.agreementBT setTitle:@"  合约协议  " forState:UIControlStateNormal];
    [self.agreementBT setTitleColor:[YZMAppStyle colorForBlueText] forState:UIControlStateNormal];
    [self.agreementBT.titleLabel setFont:[YZMAppStyle smallSizeFont]];
    [self.agreementBT addTarget:self action:@selector(clickToShowAgreement:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:self.agreementBT];
    [self.agreementBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.right.equalTo(view);
    }];
    
    return view;
}


+ (CGFloat)heightWithRecord:(YZMSaleTradeModel *)record{
    
    CGFloat height = 12 + 4 + 21 + 5 + 31 + 20 + 10 + 24;
    
    UILabel *largeLB = [UILabel new];
    largeLB.font = [YZMAppStyle largeSizeFont];
    largeLB.text = @"测试";
    [largeLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
    }];
    
    height += [largeLB systemLayoutSizeFittingSize:[UIScreen mainScreen].bounds.size].height;
    
    UILabel *middleLB = [UILabel new];
    middleLB.font = [YZMAppStyle fontWithSize:14.0];
    middleLB.text = @"测试";
    [middleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
    }];
    
    height += [middleLB systemLayoutSizeFittingSize:[UIScreen mainScreen].bounds.size].height*3;
    
    if (record.status == YZMSaleTradeStausForHolding) {
        
        height += 20;
    }
    
    return height;
}


- (YZMAlertView *)alertViewForApartSellWithList:(NSArray *)apartList andTotalAmount:(NSInteger)totalAmount andApartAmount:(NSInteger)apartAmount{
    
    YZMAlertView *alertView = [YZMAlertView new];
    alertView.contentLabel.text = [NSString stringWithFormat:@"行情波动过大，造成部分成交，原持有数量%@股，现剩余%@股，请处理剩余数量，以便结算！",@(totalAmount), @(apartAmount)];
    alertView.contentLabel.textColor = [YZMAppStyle colorForBlackText];
    alertView.contentLabel.font = [YZMAppStyle mediumSizeBoldFont];
    alertView.cancelButton.hidden = YES;
    
    UIView *stackView = [UIView new];
    
    NSMutableArray *leftArray = [NSMutableArray arrayWithObjects:@"成交时间", nil];
    NSMutableArray *middleArray = [NSMutableArray arrayWithObjects:@"数量", nil];
    NSMutableArray *rightArray = [NSMutableArray arrayWithObjects:@"价格", nil];
    
    for (YZMSaleTradeModelForApart *apart in apartList) {
        
        [leftArray addObject:[apart.time formattedDateWithFormat:@"YYYY-MM-dd"]];
        [middleArray addObject:[NSString stringWithFormat:@"%@",@(apart.amount)]];
        [rightArray addObject:@(apart.price).p02fString];
    }
    
    NSArray *array = @[leftArray, middleArray, rightArray];
    [array enumerateObjectsUsingBlock:^(NSArray *obj, NSUInteger idx, BOOL *stop) {
        
        UIView *subStackView = [UIView new];
        [stackView addSubview:subStackView];
        [subStackView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(stackView);
            make.top.equalTo(stackView).offset(10);
            if (idx == 0) {
                make.left.equalTo(stackView);
            }else if(idx == 1){
                
                make.centerX.equalTo(stackView).offset(20);
                
            }else{
                
                make.right.equalTo(stackView);
            }
        }];
        
        
        UIView *lastView = nil;
        BOOL flag = YES;
        for (NSString *string in obj) {
            UILabel *label = [YZMAppStyle labelWithMediumGray];
            label.text = string;
            
            [subStackView addSubview:label];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                
                if (idx == 0) {
                    
                    make.left.equalTo(subStackView);
                    make.right.lessThanOrEqualTo(subStackView);
                    
                }else{
                    
                    make.left.greaterThanOrEqualTo(subStackView);
                    make.right.equalTo(subStackView);
                }
                
                if (lastView) {
                    
                    make.top.equalTo(lastView.mas_bottom).offset(flag?8:2);
                    
                }else{
                    make.top.equalTo(subStackView);
                }
            }];
            
            if (lastView) {
                flag = NO;
            }
            lastView = label;
            
        }
        
        if (idx == 0) {
            
            [subStackView mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.bottom.equalTo(lastView);
            }];
        }
        
    }];
    
    
    UIView *splitLine = [YZMAppStyle splitLine];
    [stackView addSubview:splitLine];
    [splitLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(stackView).offset(-2);
        make.left.equalTo(stackView).offset(-4);
        make.right.equalTo(stackView).offset(4);
    }];
    
    alertView.customView = stackView;
    
    return alertView;
}



@end
